package com.qf.electronic.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.electronic.common.R;
import com.qf.electronic.common.ResponseStatus;
import com.qf.electronic.dto.DispatchDto;
import com.qf.electronic.dto.InspectionDto;
import com.qf.electronic.dto.InspectionStateDto;
import com.qf.electronic.pojo.Inspection;
import com.qf.electronic.pojo.User;
import com.qf.electronic.service.IInspectionService;
import com.qf.electronic.vo.InspectionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wu
 * @since 2023-05-04
 */
@RestController
@RequestMapping("/inspection")
public class InspectionController {

    @Autowired
    private IInspectionService inspectionService;

    @PostMapping
    public R<Boolean> addInspection(@Valid @RequestBody Inspection inspection, BindingResult result){
        if(result.hasFieldErrors()){
            return R.error(ResponseStatus.INVALID_PARAMETER);
        }
        return R.ok(inspectionService.save(inspection));
    }

    @PutMapping
    public R<Boolean> updateInspection(@Valid @RequestBody Inspection inspection, BindingResult result){
        if(result.hasFieldErrors()){
            return R.error(ResponseStatus.INVALID_PARAMETER);
        }
        if(StringUtils.hasLength(inspection.getInspectionUser())){//说明是分配了巡检员的
            inspection.setState(1);
        }
        return R.ok(inspectionService.updateById(inspection));
    }

    @PutMapping("/dispatch")
    public R<Boolean> dispatchInspection(@RequestBody DispatchDto dispatchDto){
        return R.ok(inspectionService.dispatchInspection(dispatchDto));
    }

    @PutMapping("/state")
    public R<Boolean> changeInspectionState(@RequestBody InspectionStateDto stateDto){
        UpdateWrapper<Inspection> wrapper = new UpdateWrapper<>();
        wrapper.set("state", stateDto.getState());
        wrapper.eq("id", stateDto.getId());
        return R.ok(inspectionService.update(wrapper));
    }

    @GetMapping
//    @PreAuthorize("hasAnyRole('ROLE_LINE', 'ROLE_INSPECTION')")
    public R<Page<InspectionVo>> searchInspections(InspectionDto inspectionDto){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        Integer roleId = user.getRoleId();
        if(roleId == 3){
            inspectionDto.setOwner(user.getUsername());
        }
        return R.ok(inspectionService.searchInspections(inspectionDto));
    }
}

